<?
       
		require_once('../../Inc/Global.php');
        require_once('UserData.php');
        include_once('../../MembersOnly/Authentication.php');
        if(isset($_REQUEST['Submit']))
        {
                $rs = $_REQUEST;
                if(!$errors = ValidateUser($rs))
                {
                	if(isset($_REQUEST['ID']))
                	{
                		if(!$errors = SaveUser($rs))
                		{
                    		header("location: index.php");
                		}
                	}
                	else
                	{
                		if(!$errors = AddUser($rs))
                  		{
                    		header("location: index.php");
                		}
                		
                	}
                }
               
        }else{
        		if(isset($_REQUEST['ID']))
        		{
                	$rs = GetUser($_REQUEST['ID']);
        		}
        		else
        		{
        			$rs = NewUser();
        			# Create a blank associative array -> set it up this way for error handling
        		}
        }
?>

<!DOCTYPE html>
<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Create/Edit User: <?=$rs['First_Name']?> <?=$rs['Last_Name']?></title>
        <link rel="stylesheet" type="text/css" href="../../ECommSite/ECommCss.css" />
</head>
<body>
  <table>      <caption>Add/Edit User</caption>
        <form method="post">
               			
               			<?if(GetUserClearance() == 'Administrator')
				        {?>  
                        <tr>
                        <td>
                                <label>ID:</label>
                                <?=$rs['ID']?>
                        </td>
                        </tr>
                        <br />
                        <?}?>
                        <tr>
                        <td>
                                <label for="First_Name">First Name:</label>
                                <input  type="text" name="First_Name" id="First_Name"
                                                class="<? if(isset($errors['First_Name'])){ ?>error<? } ?>"
                                                value="<?=$rs['First_Name']?>" />
                                                <? if(isset($errors['First_Name'])){ ?>
                                                        <span class="error"><?=$errors['First_Name']?></span>
                                                <? } ?>
                        </td>
                        </tr>                  
                        <tr>
                        <td>
                                <label for="Last_Name">Last Name:</label>
                                <input  type="text" name="Last_Name" id="Last_Name"
                                                class="<? if(isset($errors['Last_Name'])){ ?>error<? } ?>"
                                                value="<?=$rs['Last_Name']?>" />
                                                <? if(isset($errors['Last_Name'])){ ?>
                                                        <span class="error"><?=$errors['Last_Name']?></span>
                                                <? } ?>
                        </td>
                        </tr>
                        <tr>
                        <td>
                                <label for="Address_Line_1">Address Line 1:</label>
                                <input  type="text" name="Address_Line_1" id="Address_Line_1"
                                                class="<? if(isset($errors['Address_Line_1'])){ ?>error<? } ?>"
                                                value="<?=$rs['Address_Line_1']?>" />
                                                <? if(isset($errors['Address_Line_1'])){ ?>
                                                        <span class="error"><?=$errors['Address_Line_1']?></span>
                                                <? } ?>
                        </td>
                        </tr>
                        <tr>
                        <td>
                                <label for="Adress_Line2">Address Line 2:</label>
                                <input  type="text" name="Address_Line2" id="Address_Line2"
                                value="<?=$rs['Address_Line2']?>" />   
                        </td>
                        </tr>
                        <tr>
                        <td>
                        		<label for="City">City:</label>
                                <input  type="text" name="City" id="City"
                                                class="<? if(isset($errors['City'])){ ?>error<? } ?>"
                                                value="<?=$rs['City']?>" />
                                                <? if(isset($errors['City'])){ ?>
                                                        <span class="error"><?=$errors['City']?></span>
                                                <? } ?>
                        </td>                       
                        </tr>  
                        <tr>
                        <td>
                       			<label for="State">State:</label>
                                <select name="State">
                                	<option value=""></option>
									<option value="AL">Alabama</option>
									<option value="AK">Alaska</option>
									<option value="AZ">Arizona</option>
									<option value="AR">Arkansas</option>
									<option value="CA">California</option>
									<option value="CO">Colorado</option>
									<option value="CT">Connecticut</option>
									<option value="DE">Delaware</option>
									<option value="DC">District of Columbia</option>
									<option value="FL">Florida</option>
									<option value="GA">Georgia</option>
									<option value="HI">Hawaii</option>
									<option value="ID">Idaho</option>
									<option value="IL">Illinois</option>
									<option value="IN">Indiana</option>
									<option value="IA">Iowa</option>
									<option value="KS">Kansas</option>
									<option value="KY">Kentucky</option>
									<option value="LA">Louisiana</option>
									<option value="ME">Maine</option>
									<option value="MD">Maryland</option>
									<option value="MA">Massachusetts</option>
									<option value="MI">Michigan</option>
									<option value="MN">Minnesota</option>
									<option value="MS">Mississippi</option>
									<option value="MO">Missouri</option>
									<option value="MT">Montana</option>
									<option value="NE">Nebraska</option>
									<option value="NV">Nevada</option>
									<option value="NH">New Hampshire</option>
									<option value="NJ">New Jersey</option>
									<option value="NM">New Mexico</option>
									<option value="NY">New York</option>
									<option value="NC">North Carolina</option>
									<option value="ND">North Dakota</option>
									<option value="OH">Ohio</option>
									<option value="OK">Oklahoma</option>
									<option value="OR">Oregon</option>
									<option value="PA">Pennsylvania</option>
									<option value="RI">Rhode Island</option>
									<option value="SC">South Carolina</option>
									<option value="SD">South Dakota</option>
									<option value="TN">Tennessee</option>
									<option value="TX">Texas</option>
									<option value="UT">Utah</option>
									<option value="VT">Vermont</option>
									<option value="VA">Virginia</option>
									<option value="WA">Washington</option>
									<option value="WV">West Virginia</option>
									<option value="WI">Wisconsin</option>
									<option value="WY">Wyoming</option>
                        </td>
                        </tr>
                        <tr>
                        <td>
                        		<label for="Zipcode">Zipcode:</label>
                                <input  type="text" name="Zipcode" id="Zipcode"
                                                class="<? if(isset($errors['Zipcode'])){ ?>error<? } ?>"
                                                value="<?=$rs['Zipcode']?>" />
                                                <? if(isset($errors['Zipcode'])){ ?>
                                                        <span class="error"><?=$errors['Zipcode']?></span>
                                                <? } ?>
                        </td>
                        </tr> 
                        <tr><td height="6"></td></tr>  
                       	<tr>
                       	<td>
                       			<label for="Phone">Phone:</label>
                                <input  type="text" name="Phone" id="Phone"
                                                class="<? if(isset($errors['Phone'])){ ?>error<? } ?>"
                                                value="<?=$rs['Phone']?>" />
                                                <? if(isset($errors['Phone'])){ ?>
                                                        <span class="error"><?=$errors['Phone']?></span>
                                                <? } ?>
                       	</td>
                       	</tr>
                       	
                        <tr>
                       	<td>
                       			<label for="Email">Email:</label>
                                <input  type="text" name="Email" id="Email"
                                                class="<? if(isset($errors['Email'])){ ?>error<? } ?>"
                                                value="<?=$rs['Email']?>" />
                                                <? if(isset($errors['Email'])){ ?>
                                                        <span class="error"><?=$errors['Email']?></span>
                                                <? } ?>
                                              
                       	</td>
                       	
                       	</tr>     
                        <tr><td>This will be your login username.</td> </tr>         
                        <tr>
                        <td>
                                <label for="Password">Password:</label>
                                <input  type="text" name="Password" id="Password"
                                                class="<? if(isset($errors['Password'])){ ?>error<? } ?>"
                                                value="<?=$rs['Password']?>" />
                                                <? if(isset($errors['Password'])){ ?>
                                                        <span class="error"><?=$errors['Password']?></span>
                                                <? } ?>
                        
                        </td>
                        
                        </tr>
                        
                         <?if(GetUserClearance() == 'Administrator')
				        	{  
				        	?>      
				        	<tr>
				        	<td>
							<label for="SecurityLevel">Security Level:</label>
							<select name="SecurityLevel" id="SecurityLevel">
								  	<option value="Administrator">Administrator</option>
								    <option value="Customer" selected="selected">Customer</option>
                                
                        	</select>                 
                        	</td>
                        	</tr>
				            
				           <?}?>
				        
						<?if(GetUserClearance() == 'Administrator')
				        {?>  
                        <tr>
                        <td>
                                <label>Creation Date:</label>
                                <?=$rs['Created_at']?>
                                
                        </td>
                        </tr>
                        <tr>
                        <td>
                                <label>Last Updated At:</label>
                                <?=$rs['Updated_at']?>
                        </td>
                        </tr>
                        <?}?>
                        
                        <br />
                         </form>
                        </table>
                        <input type="submit" name="Submit" value="Submit" />
                        <input type="button" value="Cancel" onclick="window.location.href='http://cs.newpaltz.edu/~N00001387/WebClass/WebClass/DatabaseProject/UserManagement/'" />
                    
                        <br />
                        <input type="button" value="Go To Home Screen" onclick="window.location.href='http://cs.newpaltz.edu/~N00001387/WebClass/WebClass/ECommSite/Admin.php'" />
       
</body>
</html>

